package com.example.show_auto.ui.theme

import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.material3.CardColors
import com.example.show_auto.ui.utils.DeviceCompatibility
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext

private val DarkColorScheme = darkColorScheme(
    primary = Blue80,
    secondary = BlueGrey80,
    tertiary = Cyan80,
    background = BackgroundDark,
    surface = SurfaceDark,
    onPrimary = Color.Black,
    onSecondary = Color.Black,
    onTertiary = Color.Black,
    onBackground = OnBackgroundDark,
    onSurface = OnSurfaceDark,
    primaryContainer = Color(0xFF2E3440),
    onPrimaryContainer = Color(0xFFE5E9F0),
    secondaryContainer = Color(0xFF3B4252),
    onSecondaryContainer = Color(0xFFD8DEE9),
    surfaceVariant = Color(0xFF434C5E),
    onSurfaceVariant = Color(0xFFECEFF4),
    outline = Color(0xFF5E81AC),
    outlineVariant = Color(0xFF4C566A)
)

private val LightColorScheme = lightColorScheme(
    primary = Blue40,
    secondary = BlueGrey40,
    tertiary = Cyan40,
    background = BackgroundLight,
    surface = SurfaceLight,
    onPrimary = Color.White,
    onSecondary = Color.White,
    onTertiary = Color.White,
    onBackground = OnBackgroundLight,
    onSurface = OnSurfaceLight,
    primaryContainer = Color(0xFFE3F2FD),
    onPrimaryContainer = Color(0xFF0D47A1),
    secondaryContainer = Color(0xFFECEFF1),
    onSecondaryContainer = Color(0xFF263238),
    surfaceVariant = Color(0xFFF5F5F5),
    onSurfaceVariant = Color(0xFF424242),
    outline = Color(0xFF9E9E9E),
    outlineVariant = Color(0xFFE0E0E0)
)

@Composable
fun ShowautoTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }

        darkTheme -> DarkColorScheme
        else -> LightColorScheme
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}

/**
 * 获取与厨师选择框一致的背景色
 */
@Composable
fun getChefBoxCompatibleCardColors(): CardColors {
    return DeviceCompatibility.getAdaptiveCardColors()
}

/**
 * 获取稍深一些的背景色（用于层次区分）
 */
@Composable
fun getDarkGreyBackgroundColor(): Color {
    return MaterialTheme.colorScheme.surfaceVariant
}